﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using XDerive.XStudio.SystemFramework.Helper;
using Microsoft.Win32;
using XDerive.XStudio.SystemFramework;

namespace WebStudio.Mvc.Core.Helper
{
    /// <summary>
    /// 服务器信息工具类
    /// </summary>
    public class ServerHelper
    {
        /// <summary>
        /// 获取服务器IIS版本
        /// </summary>
        /// <param name="DomainName"></param>
        /// <returns></returns>
        public static IISVersion GetIISVersion()
        {
            IISVersion type = IISVersion.Unknown;
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp");
                var version = SysFunction.StringParse(key.GetValue("MajorVersion"));
                switch (version)
                {
                    case "5":
                        type = IISVersion.IIS5;
                        break;
                    case "6":
                        type = IISVersion.IIS6;
                        break;
                    case "7":
                        type = IISVersion.IIS7;
                        break;
                }
            }
            catch (Exception e)
            {
                ExceptionHelper.Handle(e);
            }
            return type;

            //    LoggerHelper.WriteWarning("version=" + version);
            //    string domainName = "LOCALHOST";
            //    string path = String.Format("IIS://{0}/W3SVC/INFO", domainName);
            //    DirectoryEntry entry = null;
            //    try
            //    {
            //        entry = new DirectoryEntry(path);
            //    }
            //    catch
            //    {
            //        return WebServerTypes.Unknown;
            //    }
            //    int num = 5;
            //    try
            //    {
            //        LoggerHelper.WriteWarning("num22=" + entry.Properties["MajorIISVersionNumber"].Value);
            //        num = (int)entry.Properties["MajorIISVersionNumber"].Value;
            //    }
            //    catch
            //    {
            //        return WebServerTypes.IIS5;
            //    }
            //    switch (num)
            //    {
            //        case 6:
            //            return WebServerTypes.IIS6;

            //        case 7:
            //            return WebServerTypes.IIS7;
            //    }
            //    LoggerHelper.WriteWarning("num=" + num);
            //    return WebServerTypes.IIS6;
        }
    }

    /// <summary>
    /// IIS类型枚举
    /// </summary>
    public enum IISVersion
    {
        /// <summary>
        /// 未知
        /// </summary>
        Unknown = 0,

        /// <summary>
        /// IIS5
        /// </summary>
        IIS5 = 5,

        /// <summary>
        /// IIS6
        /// </summary>
        IIS6 = 6,

        /// <summary>
        /// IIS7
        /// </summary>
        IIS7 = 7
    }
}
